<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/20
 * Time: 14:56
 */

namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\User;
use App\Models\Topic;
use Carbon\Carbon;
use DB;

class CalculateUserRank extends Command
{
    protected $signature = 'dspstack:calculate-user-rank';
    protected $description = 'Calculate user rank';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $this->calculateUserRank();
    }

    protected function calculateUserRank()
    {
        $user_list = User::query()->select('id','view_count', 'blog_rank')
            ->orderBy('view_count', 'DESC')
            ->get();

        foreach ($user_list as $key => $user)
        {
            $user->blog_rank = $key + 1;
            $user->save();
        }
    }


}